# kCloud Frontend Build Script for Windows

Write-Host "🚀 Starting kCloud Frontend Build..." -ForegroundColor Green

# 检查Node.js版本
Write-Host "📋 Checking Node.js version..." -ForegroundColor Yellow
node --version
npm --version

# 清理之前的构建
Write-Host "🧹 Cleaning previous build..." -ForegroundColor Yellow
if (Test-Path "dist") {
    Remove-Item -Recurse -Force "dist"
}

# 安装依赖
Write-Host "📦 Installing dependencies..." -ForegroundColor Yellow
npm ci

if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ Failed to install dependencies!" -ForegroundColor Red
    exit 1
}

# 类型检查
Write-Host "🔍 Type checking..." -ForegroundColor Yellow
npm run type-check

if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ Type check failed!" -ForegroundColor Red
    exit 1
}

# 构建生产版本
Write-Host "🏗️ Building for production..." -ForegroundColor Yellow
npm run build

if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ Build failed!" -ForegroundColor Red
    exit 1
}

# 检查构建结果
if (Test-Path "dist") {
    Write-Host "✅ Build completed successfully!" -ForegroundColor Green
    Write-Host "📊 Build statistics:" -ForegroundColor Cyan
    
    $distSize = (Get-ChildItem -Recurse "dist" | Measure-Object -Property Length -Sum).Sum / 1MB
    Write-Host "Total size: $([math]::Round($distSize, 2)) MB" -ForegroundColor Cyan
    
    $fileCount = (Get-ChildItem -Recurse "dist" -Include "*.js", "*.css", "*.html").Count
    Write-Host "Files generated: $fileCount" -ForegroundColor Cyan
} else {
    Write-Host "❌ Build failed - dist directory not found!" -ForegroundColor Red
    exit 1
}

Write-Host "🎉 kCloud Frontend build process completed!" -ForegroundColor Green
